import 'dart:math';

import 'package:flutter/material.dart';
import 'package:mqtt_client/mqtt_client.dart';
import 'package:ncov_2019/beans/Entity.dart';
import 'package:ncov_2019/mqtt/Mqtt.dart';

import 'machine_settings.dart';

class TestDemo extends StatefulWidget {

  final App_esp_ip model;

  const TestDemo({Key key, this.model}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TestDemoState();
  }
}

class _TestDemoState extends State<TestDemo>
    with SingleTickerProviderStateMixin {
  TabController mController;
  List<String> tabTitles;

  Mqtt demo = Mqtt.getInstance();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _appBarView(),
      body: _tabBarView(),
    );
  }

  @override
  void dispose() {
    super.dispose();
    mController.dispose();
  }

  @override
  void initState() {
    super.initState();

    print(widget.model);

    demo.connect();

    tabTitles = [
      "参数设置",
      "开关控制",
    ];

    mController = TabController(
      length: tabTitles.length,
      vsync: this,
    );
  }

  Widget _appBarView() {
    return AppBar(
      centerTitle: true,
      title: _tabBar(),
    );
  }

  Widget _tabBar() {
    return TabBar(
        //设置tab是否可水平滑动
        isScrollable: true,
        //控制器
        controller: mController,
        //设置tab文字得类型
        labelStyle: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
        //设置tab选中得颜色
        labelColor: Colors.black,
        //设置tab未选中得颜色
        unselectedLabelColor: Colors.black45,
        //设置自定义tab的指示器，CustomUnderlineTabIndicator
        //若不需要自定义，可直接通过
        //indicatorColor 设置指示器颜色
        //indicatorWight 设置指示器厚度
        //indicatorPadding
        //indicatorSize  设置指示器大小计算方式
//        indicator: CustomUnderlineTabIndicator(
//            strokeCap: StrokeCap.round,
//            insets: EdgeInsets.only(left: 15, right: 15),
//            borderSide: BorderSide(width: 4.0, color: Colors.red)),
        tabs: tabTitles.map((item) {
          return Tab(text: item);
        }).toList());
  }

  Widget _tabBarView() {
    return TabBarView(
      controller: mController,
      children: tabTitles.map((item) {
        if(item == "参数设置"){
          return new MachineSettings();
        }else{
          return Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                FlatButton(
                  textColor: Colors.black,
                  onPressed: (){
                    final MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();
//                    builder.addInt(1);
//                    builder.addInt(3);
//                    builder.addInt(1);
//                    builder.addInt(3);
//                    builder.addInt(3);

                      builder.addByte(0x48);
                      builder.addByte(0x4F);
                      builder.addByte(0x50);
                      builder.addByte(0x45);
                      builder.addByte(0x4E);
                      demo.publishMessage(builder);
                  },
                  child: Row(
                    children: <Widget>[
                      Icon(Icons.settings,color: Colors.blueAccent,),
                      Text('开门',style: TextStyle(fontSize: 15,fontWeight: FontWeight.bold),)
                    ],
                  ),
                ),
                FlatButton(
                  textColor: Colors.black,
                  onPressed: (){
                    final MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();
//                    builder.addInt(1);
//                    builder.addInt(3);
//                    builder.addInt(1);
//                    builder.addInt(3);
//                    builder.addInt(3);
                    builder.addByte(0x48);
                    builder.addByte(0x4F);
                    builder.addByte(0x4C);
                    builder.addByte(0x53);
                    builder.addByte(0x45);
                    demo.publishMessage(builder);
                  },
                  child: Row(
                    children: <Widget>[
                      Icon(Icons.assignment_late,color: Colors.orange,),
                      Text('关门',style: TextStyle(fontSize: 15,fontWeight: FontWeight.bold),)
                    ],
                  ),
                ),
              ],
            ),
//            child: Center(
//              child: Text(item,
//                  style: TextStyle(
//                      fontSize: 18,
//                      fontWeight: FontWeight.bold,
//                      color: Colors.teal)),
//            ),
          );
        }
        
      }).toList(),
    );
  }



}